using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Data.SqlClient;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.IO;
using SiteManager;


public partial class Admin_reports_Ad_DentistExportToXls : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        System.String eDt = Request.QueryString["dt"].ToString().Trim();
        try
        {

            if (eDt.Trim().Length != 0)
            {
                System.String strCriteria = Request.QueryString["crt"].ToString().Trim();  // to split a string into corresponding parameter values for the procedure 
                System.String eFlg = strCriteria.Substring(0, 1);                          // like DAPM string will split down into (D) for doctor, (DAP) Doctor Accessed Profile, & (M) for Monthly
                System.String rType = strCriteria.Substring(0, 3);
                System.String cFlg = strCriteria.Substring(3, 1);
                System.String grdName = BindGrid(eDt, "E", eFlg, rType, cFlg);  // Need to capture the grid to bind it to the excelsheet i.e ProfileGrid(Doctors) or UserGrid(Users)
                ExportGridView(grdName);
            }
            else
            {
                Response.Write("Request Failed : Date=<b><font color=red>" + eDt + "</font></b> <br/>Invalid Date Format");
                ProfileGrid.Visible = false;
            }


        }
        catch (Exception)
        {
            Response.Write("Request Failed : Date=<b><font color=red>" + eDt + "</font></b> <br/>Invalid Date Format");
            ProfileGrid.Visible = false;
        }

    }
    protected string BindGrid(string eDt, string fbFlg, string eFlg, string rType, string cFlg)
    {
        Manager mgr = new Manager(true,"ConnectionString");
        SqlDataReader drRep;
        SqlParameter[] sp_Array = new SqlParameter[5];
        System.String grdName = string.Empty;
        sp_Array[0] = new SqlParameter("@EntryDt", SqlDbType.Char);
        sp_Array[1] = new SqlParameter("@FBflag", SqlDbType.Char);
        sp_Array[2] = new SqlParameter("@Entity_flag", SqlDbType.Char);
        sp_Array[3] = new SqlParameter("@Rpt_type", SqlDbType.Char);
        sp_Array[4] = new SqlParameter("@Criteria_flag", SqlDbType.Char);

        sp_Array[0].Size = 10;
        sp_Array[1].Size = 1;
        sp_Array[1].Size = 1;
        sp_Array[1].Size = 5;
        sp_Array[1].Size = 1;

        sp_Array[0].Value = eDt;
        sp_Array[1].Value = fbFlg;
        sp_Array[2].Value = eFlg;
        sp_Array[3].Value = rType;
        sp_Array[4].Value = cFlg;

        drRep = mgr.ExecuteQuery("sp_DentistSiteAnalytics", sp_Array);

       if (eFlg == "D")  // Doctors
        {
            ProfileGrid.DataSource = drRep;
            ProfileGrid.DataBind();
            grdName = "ProfileGrid";
        }

        return grdName;
    }


    // Exporting Data To Excel Starts from here


    private void ExportGridView(System.String grdName)
    {
        System.String sheetName = string.Empty;  // Required to change the sheet name based on Criteria (Doctor or User)
        if (grdName == "ProfileGrid")
        {
            sheetName = "DentistList.xls";
        }
        Response.ClearContent();
        Response.AddHeader("content-disposition", "attachment;filename=" + sheetName);


        Response.ContentType = "application/vnd.xls";

        System.IO.StringWriter stringWrite = new System.IO.StringWriter();

        System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

        GridView grdObj = new GridView();
        grdObj = (GridView)Page.FindControl(grdName);
        grdObj.RenderControl(htmlWrite);

        Response.Write(stringWrite.ToString());

        //Response.End();

    }

    public override void VerifyRenderingInServerForm(Control control)
    {

    }

}
